package com.example.hzq.zqb.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageButton;

import com.example.hzq.zqb.R;
import com.example.hzq.zqb.fragment.CampusshopFragment;
import com.example.hzq.zqb.fragment.PagerSlidingTabStrip;

/**
 * Created by hzq on 2017/7/6.
 */

public class CampusshopActivity extends FragmentActivity {
    private CampusshopFragment all=null,book=null,digital=null,clothes=null,store=null,Ican=null,Do=null,used=null;
    private PagerSlidingTabStrip pagersliding;
    private ViewPager viewpager;
    private DisplayMetrics dm;
    private ImageButton ib;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.campusshop);
        init();
    }

    private void init() {
        dm = getResources().getDisplayMetrics();//获取屏幕密度
        viewpager = (ViewPager) findViewById(R.id.viewpage2);
        viewpager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        pagersliding = (PagerSlidingTabStrip) findViewById(R.id.pagersliding1);
        pagersliding.setViewPager(viewpager);
        ib=(ImageButton)findViewById(R.id.back);
        ib.setImageResource(R.mipmap.bk);
        ib.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setpagerstyle();//设置PagerSlidingTabStrip显示效果
    }

    private void setpagerstyle() {
        pagersliding.setShouldExpand(true); // 设置Tab是自动填充满屏幕的
        pagersliding.setDividerColor(Color.TRANSPARENT); // 设置Tab的分割线是透明的
        // 设置Tab底部线的高度
        pagersliding.setUnderlineHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 1, dm));
        // 设置Tab Indicator的高度
        pagersliding.setIndicatorHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 4, dm));
        // 设置Tab标题文字的大小
        pagersliding.setTextSize((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 16, dm));
        pagersliding.setIndicatorColor(Color.parseColor("#45c01a"));// 设置Tab Indicator的颜色
        // 设置选中Tab文字的颜色 (这是自定义的一个方法)
        pagersliding.setSelectedTextColor(Color.parseColor("#45c01a"));
        pagersliding.setTabBackground(0); // 取消点击Tab时的背景色
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        private String[] titles = {"全部", "闲置课本","文体数码","服装","日用百货","我能","代劳","二手"};//显示在二级导航上的标题文字
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
           switch (position){
               case 0:
                   if (all==null)
                       all=new CampusshopFragment(1);
                   return all;
               case 1:
                   if (book==null)
                       book=new CampusshopFragment(2);
                   return book;
               case 2:
                   if (digital==null)
                       digital=new CampusshopFragment(3);
                   return digital;
               case 3:
                   if (clothes==null)
                       clothes=new CampusshopFragment(4);
                   return clothes;
               case 4:
                   if (store==null)
                       store=new CampusshopFragment(5);
                   return store;
               case 5:
                   if (Ican==null)
                       Ican=new CampusshopFragment(6);
                   return Ican;
               case 6:
                   if (Do==null)
                       Do=new CampusshopFragment(7);
                   return Do;
               case 7:
                   if (used==null)
                       used=new CampusshopFragment(8);
                   return used;
                default:
                    return null;
           }
        }
        @Override
        public int getCount() {
            return titles.length;
        }
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }
}
